package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.Menuimage;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class FindFragmentAdpter extends
        RecyclerView.Adapter<FindFragmentAdpter.fragmentAdpter> {

    private List<MenuResult> m;
    private Menuimage mlistener;

    public void addData(List<MenuResult> result) {
        m = result;
        notifyDataSetChanged();
    }

    public void setImage(Menuimage listener) {
        mlistener = listener;
    }

    @Override
    public fragmentAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.rececy_beauty_item, null);

        return new fragmentAdpter(view);
    }

    @Override
    public void onBindViewHolder(fragmentAdpter holder, int position) {
        if (m != null) {

            MenuResult result = m.get(position);
            if (result != null) {
                holder.findName.setText(result.getName());
                //  holder.need.setText(result.getFood());

                //拆分字符串
                String[] strarray = result.getKeywords().split("[ ]");
                for (int i = 0; i < strarray.length; i++)
                    System.out.println(strarray[i]);
                {
                    String k = strarray[0];
                    String k2 = strarray[1];
                    String k3 = strarray[2];
                    String k4 = strarray[3];
                    String k5 = strarray[4];

                    holder.key.setText(k);
                    holder.keyTw.setText(k2);
                    holder.keyThree.setText(k3);
                    holder.keyFr.setText(k4);
                    holder.keyFive.setText(k5);
                }
//                holder.deSciption.setText(result.getDescription());
                if (result.getImg() != null && result.getImg().length() > 0) {
                    String imgUrl = result.getImg();
                    String url = " http://tnfs.tngou.net/image/" + imgUrl;
                    getBitmap(url, holder.img);
                }

            }

        }
        holder.position = position;
    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public int getItemCount() {
        return m != null && m.size() > 0 ? m.size() : 0;
    }

    class fragmentAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView img;
        private TextView findName, need, deSciption, searClick, key, keyTw, keyThree, keyFr, keyFive;
        private int position;

        public fragmentAdpter(View itemView) {
            super(itemView);
            img = (NetworkImageView) itemView.findViewById(R.id.titleC);
            findName = (TextView) itemView.findViewById(R.id.titlecity);

            key = (TextView) itemView.findViewById(R.id.key);
            keyTw = (TextView) itemView.findViewById(R.id.key1);
            keyThree = (TextView) itemView.findViewById(R.id.key2);
            keyFr = (TextView) itemView.findViewById(R.id.key3);
            keyFive = (TextView) itemView.findViewById(R.id.key4);

            deSciption = (TextView) itemView.findViewById(R.id.discreption);
            searClick = (TextView) itemView.findViewById(R.id.click);
            searClick.setOnClickListener(this);
            img.setOnClickListener(this);

        }

        private int count = 0;

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.click:
                    if (count == 0) {
                        if (m != null && m.size() > 0) {
                            MenuResult menuResult = m.get(position);
                            String str = menuResult.getDescription();
                            deSciption.setText(str);
                        }
                        count = 1;
                    } else if (count == 1) {
                        deSciption.setText(null);
                        count = 0;
                    } else {
                        deSciption.setText(null);
                    }

                    break;

                case R.id.titleC:
                    MenuResult menuResult = m.get(position);
                    if (mlistener != null) {
                        mlistener.MenuimageOnclick(menuResult);
                    }

                    break;
                default:
                    break;

            }
        }


    }
}
